import "./index.css";

import { React, useRef } from "react";
import axios from "axios";
import { useNavigate, useLocation } from "react-router-dom";
import { message } from "antd";

export default function BanlancePaySuccess() {
  var shuruRef = useRef();
  const loca = useLocation();

  const navigate = useNavigate();
  var onOff = true;
  function goPayNow() {
    message.loading("请耐心等待，不要刷新页面，以防数据丢失", 1);
    // 获取当前时间
    let date = new Date(+new Date() + 8 * 3600 * 1000)
      .toISOString()
      .replace(/T/g, " ")
      .replace(/\.[\d]{3}Z/, "");
    if (onOff) {
      onOff = false;
      //验证用户信息
      axios.post("http://localhost:3000/index/verify", {
        user_id: document.cookie.slice(5),
        user_pass: shuruRef.current.value,
      }).then((res) => {
        if (res.data[0].user_id === Number(document.cookie.slice(5)) && Number(res.data[0].money) >= Number(loca.state.sumMoneyAll)) {
          //扣钱请求
          if (loca.state.shops) {
            axios.post("http://localhost:3000/index/pay", {
              user_id: res.data[0].user_id,
              payMoney: loca.state.sumMoneyAll,
            }).then((res) => {
              if (res.data.msg === '你出血了') {
                message.success(res.data.msg);
                //获取订单id
                axios.post("http://localhost:3000/user/selSingleIndent", {
                  user_id: document.cookie.slice(5),
                  order_time: loca.state.date,
                  store: (loca.state.store) ? loca.state.store : '',
                }).then((res) => {
                  if (res.data) {
                    //更改订单状态
                    let order = [];
                    res.data.forEach((v) => {
                      order.push(v.order_id)
                    })
                    axios.post("http://localhost:3000/user/indentStatus", {
                      order_status: '待发货',
                      orderId: order,
                    }).then((res) => {
                      if (res.data.msg) {
                        axios.post("http://localhost:3000/user/selUser", {
                          user_id: document.cookie.slice(5),
                        }).then((res) => {
                          if (res.data[0].money) {
                            axios.post("http://localhost:3000/user/addBill", {
                              bill_type: '消费',
                              consume_money: loca.state.sumMoneyAll,
                              balance: res.data[0].money,
                              consume_time: date,
                              user_id: document.cookie.slice(5),
                            }).then((res) => {
                              navigate("/home/paysuccess", { state: loca.state });
                            });
                          }
                        });
                      }
                    });
                  }
                });
              }
            });
          } else {
            // TODO: 积分商城
            axios.post("http://localhost:3000/index/payIntegral", {
              user_id: res.data[0].user_id,
              payMoney: loca.state.sumMoneyAll,
            }).then((res) => {
              if (res.data.msg === '你出血了') {
                message.success(res.data.msg);
                //获取订单id
                axios.post("http://localhost:3000/user/selSingleIndent/integral", {
                  user_id: document.cookie.slice(5),
                  order_time: loca.state.date,
                  store: (loca.state.store) ? loca.state.store : '',
                }).then((res) => {
                  if (res.data) {
                    //更改订单状态
                    let indent = [];
                    res.data.forEach((v) => {
                      indent.push(v.indent_id)
                    })
                    axios.post("http://localhost:3000/user/integralStatus", {
                      order_status: '待发货',
                      indent_id: indent,
                    }).then((res) => {
                      if (res.data.msg) {
                        axios.post("http://localhost:3000/user/selUser", {
                          user_id: document.cookie.slice(5),
                        }).then((res) => {
                          if (res.data[0].integral) {
                            axios.post("http://localhost:3000/user/addBill/integral", {
                              bill_type: '消费',
                              consume_money: loca.state.sumMoneyAll,
                              balance: res.data[0].integral,
                              consume_time: date,
                              user_id: document.cookie.slice(5),
                            }).then((res) => {
                              navigate("/home/paysuccess", { state: loca.state });
                            });
                          }
                        });
                      }
                    });
                  }
                });
              }
            });

          }
        } else {
          message.warning('没钱了，去充点钱吧!!!')
        }


      });
    }else{
      message.warning('别点了,再点多扣钱的!!!')
    }


  }
  return (
    <div className="tyc_payyue">
      <input
        type="text"
        ref={shuruRef}
        placeholder="请输入支付密码(用户登录密码)"
      />
      <button onClick={goPayNow}>立即支付</button>
    </div>
  );
}
